﻿/// <reference path="//Microsoft.WinJS.1.0/js/base.js" />
/// <reference path="//Microsoft.WinJS.1.0/js/ui.js" />

WinJS.Namespace.define("Controls", {
    TimeCalculator: WinJS.Class.define(function () {
        this._startDateValue = null;
        this._endDateValue = null;
        this._startTimeValue = null;
        this._endTimeValue = null;
    }, {
        _getValuesFromInputs: function () {
            var startDateInput = document.getElementById("firstDate").winControl;
            this._startDateValue = startDateInput.current.getTime();

            var endDateInput = document.getElementById("secondDate").winControl;
            this._endDateValue = endDateInput.current.getTime();

            if (document.getElementById("time").style.display != "") {
                var startDateInput = document.getElementById("startTime").winControl;
                this._startTimeValue = startDateInput.current.getTime();

                var endDateInput = document.getElementById("endTime").winControl;
                this._endTimeValue = endDateInput.current.getTime();
            }
        },

        _getDayDifference: function () {
            this._getValuesFromInputs();

            var diffInMilliSec = this._endDateValue - this._startDateValue;
            var diffInDays = diffInMilliSec / 1000 / 60 / 60 / 24;

            return diffInDays;
        },

        _getTimeDifference: function () {
            this._getValuesFromInputs();

            var diffInMilliSec = this._endTimeValue - this._startTimeValue;
            var diffInHours = diffInMilliSec / 1000 / 60 / 60;

            return Math.abs(diffInHours);
        },

        showTimePicker: function (isOn) {
            var pickersParent = document.getElementById("time");

            var startPickerParent = document.getElementById("startTime");
            var startTimePicker = new WinJS.UI.TimePicker(startPickerParent);

            var endPickerParent = document.getElementById("endTime");
            var endTimePicker = new WinJS.UI.TimePicker(endPickerParent);

            if (isOn) {
                pickersParent.style.display = "block";
            }
            else {
                pickersParent.style.display = "none";
            }
        },

        getDayAndTimeDifference: function () {
            var days = this._getDayDifference();
            var hours = this._getTimeDifference();

            if (this._startTimeValue < this._endTimeValue) {
                return days + " days and " + hours + " hours."
            }
            else {
                return (days -1) + " days and " + (24 - hours) + " hours."
            }
        }
    },
    {
        
    })
});